package com.kcht.np.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
  * @Description: 时间工具类
  * @Package:     com.kcht.np.mp.util
  * @Author:      liuqi
  * @CreateDate:  2020/3/31 23:40
 */

public class LocalTimeUtils {

    /**
     * @Description:   获取当前时间戳(秒)
     * @CreateDate:    2020/3/31 23:40
     */
    public static long getCurrentTimeSecond() {
        return LocalDateTime.now().toEpochSecond(OffsetDateTime.now().getOffset());
    }

    /**
     * @Description:   获取当前时间戳(毫秒)
     * @CreateDate:    2020/3/31 23:41
     */
    public static long getCurrentTimeMilli() {
        return LocalDateTime.now().toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
    }

    /**
     * @Description:   时间戳 (秒) 格式化 yyyy-MM-dd HH:mm:ss
     * @CreateDate:    2020/3/31 23:41
     */
    public static String timestampSecondFormat(long timestamp) {
        return LocalDateTime
                .ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()
                ).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * @Description:   时间戳(毫秒) 格式化 yyyy-MM-dd HH:mm:ss.SSS
     * @CreateDate:    2020/3/31 23:42
     */
    public static String timestampMilliFormat(long timestamp) {
        return LocalDateTime
                .ofInstant(
                        Instant.ofEpochMilli(timestamp),
                        ZoneId.systemDefault()
                ).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    }


    /**
     * @Description:   yyyy-MM-dd HH:mm:ss.SSS 转 LocalDateTime
     * @CreateDate:    2020/3/31 23:42
     */
    public static LocalDateTime stringMilliToDateTime(String datetime) {
        return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    }

    /**
     * @Description:   yyyy-MM-dd HH:mm:ss 转 LocalDateTime
     * @CreateDate:    2020/3/31 23:43
     */
    public static LocalDateTime stringSecondToDateTime(String datetime) {
        return LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
